from pathlib import Path
from ..config.config import python,workflow_dir,pub_dir
from datetime import datetime
def dealProcessShell(shellDir,samplesDir,rawSamplesDir,outdir,fqDir,runid,blastall,sextype,cpu):
    date = datetime.strftime(datetime.today(),'%Y%m%d')
    # samplesheet = Path(samplesheet).absolute()
    sextype = Path(sextype).absolute()
    # bcl2fastq sh
    # bcl2fastq = shellDir / Path('p1.bcl2fastq.sh')
    # with open(bcl2fastq, 'w') as b2f:
    #     b2f.write(f'bcl2fastq -R {bclDir} --sample-sheet {samplesheet} -o {fqDir} -p 72 --no-lane-splitting --barcode-mismatches 0 --minimum-trimmed-read-length 50 \n')
    ra = shellDir / Path('p6.nt.ra.sh')
    sex = shellDir / Path('p7.checksex.sh')

    # samples karken, rgi and blastNT
    runbwa = shellDir / Path('all.bwa.sh')
    runkraken = shellDir / Path('all.kraken.sh')
    runrgi = shellDir / Path('all.rgi.sh')
    runmyco = shellDir / Path('all.myco.sh')
    runkkbwa = shellDir / Path('all.kkbwa.sh')
    runntbwa= shellDir / Path('all.ntbwa.sh')
    kraken2files = ''
    kreport2files = ''
    # relative abundance NT
    with open(ra, 'w') as ra_out, open(sex, 'w') as sex_out,open(runbwa, 'w') as bwa, open(runkraken, 'w') as kk, open(runrgi, 'w') as rgi, open(runmyco, 'w') as myco, open(runkkbwa, 'w') as kkbwa,open (runntbwa,'w') as ntbwa:
        for k, v in samplesDir.items():
            # 写入p6.nt.ra.sh p7.checksex.sh
            samplePrefix = str(v / Path(f'{v.name}'))
            ra_out.write(f'''\
    {python} {workflow_dir}/tools/calcNTRelAbund.py -nt {samplePrefix}.*.parse.xls -info {sextype} --cofile {samplePrefix}_coverage.xls  -o {samplePrefix}.nt.ra.xls \n''')
            sex_out.write(f'''bedtools genomecov -ibam {samplePrefix}.sorted.bam -bga | {python} {workflow_dir}qcstat/checkSex.py {v.name} > {samplePrefix}.sex.xls \n''')
            # 写入
            bwash = shellDir / Path('bwa/p2.' + v.name+'.bwa.sh')
            kk2sh = shellDir / Path('kraken/p3.' + v.name+'.kraken.sh')
            rgish = shellDir / Path('rgi/p4.' + v.name+'.rgi.sh')
            mycosh = shellDir / Path('myco/p8.' + v.name+'.myco.sh')
            kkbwash = shellDir / Path('bwa/p9.' + v.name+'.kkbwa.sh')
            ntbwash= shellDir / Path('bwa/p10.'+v.name+'.ntbwa.sh')
            bwa.write(f'sh {bwash}\n')
            kk.write(f'sh {kk2sh}\n')
            rgi.write(f'sh {rgish}\n')
            myco.write(f'sh {mycosh}\n')
            kkbwa.write(f'sh {kkbwash}\n')
            ntbwa.write(f'sh {ntbwash}\n')
            samplePrefix = str(v / Path(f'{v.name}'))
        # 写入bwash脚本
            with open(bwash, 'w') as BWA:
                # BWA.write(f'''\
                BWA.write(f'''\
################ sample:{samplePrefix} ##################
/data/softwares/newalign/bwa-mem2-2.1_x64-linux/bwa-mem2 mem -t {cpu} /data/softwares/newalign/bwa-mem2-2.1_x64-linux/hg/hg38.plusNT.genomic.fna {rawSamplesDir[k]} 2>{samplePrefix}.bwa.log | samtools sort -O bam -@ {cpu} - > {samplePrefix}.sorted.bam

samtools fastq -f 4 --threads 4  {samplePrefix}.sorted.bam > {samplePrefix}.unmap.fastq

cutadapt --report=minimal -j {cpu} -b file:/data/reference/pathoseq/cutadapt/primers.fa -q 25,25 -m 49 --max-n 0 {samplePrefix}.unmap.fastq | seqkit fq2fa -j {cpu} -w 0 -o {samplePrefix}.remained.fasta -

/data/softwares/binary/bwa  mem -t 10 /data/softwares/newalign/bwa-mem2-2.1_x64-linux/plasmid/plasmid.fa {samplePrefix}.remained.fasta | samtools fasta -f 4 --threads {cpu} - > {samplePrefix}.removepla.fasta 
{python} {workflow_dir}/tools/resubmit_KK.py {samplePrefix}.removepla.fasta

#remove neibiao
/data/softwares/binary/bwa  mem -t 8 /data/reference/pathoseq/neibiao/all {samplePrefix}.removepla.fasta  | samtools sort -o {samplePrefix}.S_all.sam 
samtools fasta -f 4 {samplePrefix}.S_all.sam > {samplePrefix}.removepla_nb.fasta 
#get neibiao reads DNA CF 
cat {samplePrefix}.S_all.sam | {python} {workflow_dir}/tools/get_neibiao.py - {samplePrefix}.S_all.sam 
#remove tngs_target
/data/softwares/binary/bwa  mem -t 8 /data/reference/pathoseq/tngs_target/tngs_target.fa {samplePrefix}.removepla_nb.fasta | samtools fasta -f 4 --threads {cpu} - > {samplePrefix}.removepla_nb_tngs.fasta

seqkit rmdup -s -j {cpu} -w 0 -D {samplePrefix}.dupstat {samplePrefix}.removepla_nb_tngs.fasta > {samplePrefix}.removedupKK.fasta
perl {workflow_dir}/tools/blast100k2.pl {samplePrefix}.removedupKK.fasta {samplePrefix} {cpu} {blastall} {sextype} \n

''')
                
            with open(kk2sh, 'w') as kraken:
                kraken.write(f'''\
kraken2 -db /data/reference/pathoseq/newbwaindex/kk2index/simple/db --threads {cpu} --confidence 0 --report {samplePrefix}.kreport2 --use-names {samplePrefix}.removedupKK.fasta > {samplePrefix}.kraken2
#perl /data/softwares/mngs_scripts/kreportFilter2tab.pl --inKrakenFile {samplePrefix}.kraken2 --inKreportFile {samplePrefix}.kreport2 --outFile {samplePrefix}.species.xls --outDomainFile {samplePrefix}.domain.xls --outGenusFile {samplePrefix}.genus.xls
{python} {workflow_dir}/tools/parseKraken2.py {samplePrefix}.kraken2 {samplePrefix}.kreport2
{python} {workflow_dir}/tools/calcKKRelAbund.py -ks {samplePrefix}.species.xls -kg {samplePrefix}.genus.xls -info {sextype} -o {samplePrefix}.kk.ra.xls -nb {samplePrefix}_neibiao.xls
{python} {workflow_dir}/tools/extractKKreads.py {samplePrefix}.kraken2 {samplePrefix}.removedupKK.fasta > {samplePrefix}.kraken2.fasta \n''')
            kraken2files += f'--inKrakenFile {samplePrefix}.kraken2 '
            kreport2files += f'--inKreportFile {samplePrefix}.kreport2 '
            with open(rgish, 'w') as RGI:
                RGI.write(f'''\
#rgi load -i /data/reference/pathoseq/card-data/card.json --card_annotation /data/reference/pathoseq/card-data/nucleotide_fasta_protein_homolog_model.fasta --local
#rgi bwt --read_one {samplePrefix}.removedupKK.fasta --read_two {samplePrefix}.removedupKK.fasta --aligner bwa --local --output_file {samplePrefix} --threads {cpu}
#mv {samplePrefix}.gene_mapping_data.txt {samplePrefix}.gene_mapping_data.raw
#{python} {workflow_dir}rgi/formatRGI.py {samplePrefix}.gene_mapping_data.raw 
cd {v} && \\
bwa mem -t 18 /data/reference/pathoseq/card_2021/bin/card_kmer.fa {samplePrefix}.removedupKK.fasta | samtools sort -O bam -@ 18 - | samtools view -@ 18 - > {v.name}.txt && \\
{python} {workflow_dir}/pathoseq/s3.card_anno.py -i {v.name}.txt -outdir {v} -s {v.name} && \\
{python} {workflow_dir}/pathoseq/s4.merge_gene_family.py -i {v.name}.gene_mapping_data_bak.txt -o {v.name}.gene_mapping_data.txt 
{python} {workflow_dir}/pathoseq/s4.merge_gene_family_cn.py -i {v.name}.gene_mapping_data_bak.xls -o {v.name}.gene_mapping_data.xls \n''')
            with open(mycosh, 'w') as MYCO:
                MYCO.write(f'''\
bwa mem -t {cpu} -a -c 20 /data/reference/pathoseq/newbwaindex/Mycobacteriaceae/Mycobacteriaceae.fna {samplePrefix}.removedupKK.fasta | samtools sort -O bam -@ {cpu} - > {samplePrefix}.myco.bam
samtools view {samplePrefix}.myco.bam | {python} {workflow_dir}myco.py - {samplePrefix}.myco.stats.xls \n''')
            with open(kkbwash, 'w') as KK:
                KK.write(f'''\
#bwa mem -t {cpu} /home/runmngs/workspace/S1/neibiao {samplePrefix}.kraken2.fasta | samtools view -f 4 | samtools fasta  -  > {samplePrefix}.kraken2_t.fasta
bwa mem -t {cpu} /data/reference/pathoseq/newbwaindex/kk2index/simple/ntbwa/ntbwa  {samplePrefix}.kraken2.fasta | {python} {workflow_dir}zipKKsam.py - {samplePrefix}.litesam 
{python} {workflow_dir}/tools/calcKKRelAbund_k.py -ks {samplePrefix}.species.xls -kg {samplePrefix}.genus.xls -info {sextype} -o {samplePrefix}.kk.ra.xls -nb {samplePrefix}_neibiao.xls -co {samplePrefix}_coverage_kk.xls \n''')
            with open(ntbwash,'w') as NT:
                NT.write(f'''\
ln -sf {samplePrefix}*blastNT.fasta {samplePrefix}.NT.fasta 
ln -sf {samplePrefix}*uniqblast {samplePrefix}.UNIQ.blast
{python}  {workflow_dir}/NT/extractNT.py {samplePrefix}.NT.fasta  {samplePrefix}.UNIQ.blast
bwa mem -t {cpu} /data/reference/pathoseq/newbwaindex/kk2index/simple/ntbwa/ntbwa  {samplePrefix}_nt.fasta | {python} {workflow_dir}zipKKsam.py - {samplePrefix}.litentsam\n ''')

            # merge kraken
            mergekraken = shellDir / Path('p5.mergeKraken.sh')
            with open(mergekraken, 'w') as mk:
                mk.write(f'''\
#perl /data/softwares/mngs_scripts/kreportFilter2tab_merge.pl --outDomainFile {outdir}/mNGS.{date}.merge.domain.txt --outGenusFile {outdir}/mNGS.{date}.merge.genus.txt --outFile {outdir}/mNGS.{date}.merge.species.txt {kraken2files} {kreport2files}
#sed -i -e "s/'/_/g" -e "s/#/_/g" {outdir}/mNGS.{date}.merge.species.txt
#{python} /data/softwares/mngs_scripts/pathoseq.anno.py -s {fqDir}/{runid}.fastq.Stats.xls -i {sextype} -b /data/softwares/mngs_scripts/真核筛选.xlsx -d /data/softwares/mngs_scripts/database/ -k {outdir}/mNGS.{date}.merge.species.txt -o {outdir}/GZ.mNGS.{date}.mergeKK.xlsx
{python} {workflow_dir}NCkklist.py {fqDir}/{runid}.fastq.Stats.xls {outdir}
{python} {workflow_dir}mergeKKBGI.py {outdir} {sextype} {fqDir}/{runid}.fastq.Stats.xls /data/softwares/mngs_scripts/database/
cp */*.kk.ra.xls {pub_dir}{runid}/ra/ && cp */*gene_mapping_data*.txt {pub_dir}{runid}/rgi/ && cp */*gene_mapping_data*.txt /data/mngsSYS/b/reportTMP/1/RGI
cp */*gene_mapping_data.xls {pub_dir}{runid}/rgi/
cp fastq/{runid}.fastq.Stats.xls *.mergeKK.xlsx *.merge.KK.xls {pub_dir}{runid}/ 
cd {pub_dir} && tar zcvf {runid}.tar.gz {runid} \n''')
    return (runbwa,runkraken,runrgi,runmyco,runkkbwa,runntbwa,mergekraken,sex)
